/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package courseScheduler;

import java.sql.SQLException;

/**
 *
 * @author david
 */
public class DateNTime {
    private int day;
    private int time;
    private int id;

    private java.sql.Connection conn;

    java.sql.PreparedStatement getDNTStatement;
    java.sql.PreparedStatement setDNTStatement;
    java.sql.PreparedStatement insertDNTStatement;

    DateNTime(int newTime,int newDay){
        time = newTime;
        day = newDay;
    }

    DateNTime(java.sql.Connection newConn, int newID) throws java.sql.SQLException {
        conn = newConn;
        id = newID;
        getDNTStatement.setInt(1, id);
        java.sql.ResultSet rs = getDNTStatement.executeQuery();
        if (rs.first()){
            int count = 0;
            do {
                count ++;
            } while(rs.next());
            rs.first();
            if (count < 2){
                time = rs.getInt(1);
                day = rs.getInt(2);
            }
            else {
                System.out.println("Multiple items found");
            }
        }
        else {
            javax.swing.JOptionPane.showMessageDialog(null, "No Items found");
        }
    }

    public static int getID(java.sql.Connection newConn, int newDay, int newTime) throws java.sql.SQLException{
        java.sql.Statement stmnt = newConn.createStatement();
        java.sql.ResultSet rs = stmnt.executeQuery("SELECT id FROM date_time WHERE day_ = " + newDay + " AND time_ = " + newTime +";");
        rs.first();
        return rs.getInt(1);
    }

    public final void setTime(int newTime){
        time = newTime;
    }

    public int getTime(){
        return time;
    }

    public final void setDay(int newDay){
        day = newDay;
    }

    public int getDay(){
        return day;
    }

    public final void setID(int newID){
        id = newID;
    }

    public int getID(){
        return id;
    }

    public final void setConnection(java.sql.Connection newConn) throws java.sql.SQLException{
        conn = newConn;
        PrepareGetStmnt();
        PrepareSetStmnt();
        PrepareInsertStmnt();
    }

    private void PrepareGetStmnt() throws java.sql.SQLException{
        getDNTStatement = conn.prepareStatement("SELECT day_,time_ FROM date_time WHERE id = ?;");
    }

    private void PrepareSetStmnt() throws java.sql.SQLException{
        setDNTStatement = conn.prepareStatement("UPDATE date_time day_ = " + day + " time_ = " + time + " WHERE id = ?;");
    }

    private void PrepareInsertStmnt() throws java.sql.SQLException{
        insertDNTStatement = conn.prepareStatement("INSERT INTO date_time (day_,time_) VALUES (" + day + "," + time + ");");
    }
}
